/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * @author Fred
 */
public class LabelPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3757095996493349070L;

	Map<String, Component> components;

	Color labelColor;
	Font labelFont;

	/**
	 * @param labelFont
	 * @param labelColor
	 * @param components
	 */
	public LabelPanel(final Font labelFont, final Color labelColor, final Component... components) {
		this.components = new LinkedHashMap<String, Component>();
		this.labelFont = labelFont;
		this.labelColor = labelColor;

		for (final Component component : components) {
			final String name = component.getName();
			if (name == null) {
				throw new RuntimeException("Problem initializing label panel.  Component must have a name.");
			}
			if (component.isVisible()) {
				this.components.put(component.getName(), component);
			}
		}

		this.initComponents();
	}

	private void initComponents() {
		this.setLayout(new BorderLayout(10, 0));
		final JPanel leftPanel = new JPanel(new GridLayout(this.components.size(), 1, 20, 1));
		final JPanel rightPanel = new JPanel(new GridLayout(this.components.size(), 1, 20, 1));

		for (final String key : this.components.keySet()) {
			final JLabel label = new JLabel(key, SwingConstants.RIGHT);
			label.setFont(this.labelFont);
			label.setForeground(this.labelColor);
			leftPanel.add(label);

			rightPanel.add(this.components.get(key));
		}

		this.add(leftPanel, BorderLayout.WEST);
		this.add(rightPanel, BorderLayout.EAST);
	}
}
